package com.iwhalecloud.bss.kite.cucc.order.query;

import com.iwhalecloud.bss.kite.common.constant.KeyConsts2;
import com.iwhalecloud.bss.kite.common.util.KiteListUtils;
import com.iwhalecloud.bss.kite.dataservice.entity.OrderItem;
import com.iwhalecloud.bss.kite.dataservice.mapper.order.OrderItemMapper;
import com.iwhalecloud.bss.kite.order.client.api.query.IWithdrawOrderQueryService;
import com.ztesoft.bss.base.staticdata.CoreDataInteract;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service
public class WithdrawOrderQueryService implements IWithdrawOrderQueryService {

    @Autowired
    private OrderItemMapper orderItemMapper;

    @Override
    public List<OrderItem> listOldOrderItems(String custOrderId, String ownerCustId, List<String> askIds) {
        Example orderItemQry = new Example(OrderItem.class);
        if (KiteListUtils.isNotEmpty(askIds)) {
            orderItemQry.createCriteria().andEqualTo("belongCustId", ownerCustId).andEqualTo("custOrderId", custOrderId).andIn("askId", askIds);
        }
        else {
            orderItemQry.createCriteria().andEqualTo("belongCustId", ownerCustId).andEqualTo("custOrderId", custOrderId);
        }
        return this.orderItemMapper.selectByExample(orderItemQry);
    }

    @Override
    public List<String> listNotCommitWithdrawAskIds(List<String> askIds, String ownerCustId) {
        List<String> statusCds = Arrays.asList(CoreDataInteract.EVT_0001_101100, CoreDataInteract.EVT_0001_101300);
        return this.orderItemMapper.listWithdrawAskIds(askIds, statusCds, ownerCustId);
    }

    @Override
    public List<OrderItem> listWithdrawOrderItems(List<String> oldOrderItemIds, String ownerCustId) {
        Example orderItemQry = new Example(OrderItem.class);
        orderItemQry.createCriteria().andEqualTo("belongCustId", ownerCustId).andEqualTo("operType", CoreDataInteract.EVT_C_0003_1200).andIn("oldOrderItemId", oldOrderItemIds);
        return this.orderItemMapper.selectByExample(orderItemQry);
    }
    @Override
    public List<OrderItem> listAllOrderItems(List<String> askIds, String ownerCustId) {
        Example orderItemQry = new Example(OrderItem.class);
        orderItemQry.createCriteria().andEqualTo("belongCustId", ownerCustId).andIn("askId", askIds);
        return this.orderItemMapper.selectByExample(orderItemQry);
    }
    @Override
    public List<String> listLatterAsksNotCommit(String askId, String remark, String ownerCustId) {
        List<String> askIds = Arrays.asList(askId.split(","));
        if (KeyConsts2.CANCEL_CUST_ORDER.equals(remark)) {
            List<String> allAskIds = this.orderItemMapper.listAllAskIds(askId, ownerCustId);
            if (KiteListUtils.isEmpty(allAskIds)) {
                return askIds;
            }
            StringBuilder askIdsBuilder = new StringBuilder(askId);
            for (String askid : allAskIds) {
                askIdsBuilder.append(",").append(askid);
            }
            askId = askIdsBuilder.toString();
        }
        List<String> latterAskIds = this.orderItemMapper.listLatterNotCommitAskIds(askIds, ownerCustId);
        List<String> promotionAskIds = this.orderItemMapper.listPromotionAskIds(askId, ownerCustId);

        if (KiteListUtils.isEmpty(latterAskIds) && KiteListUtils.isEmpty(promotionAskIds)) {
            return Arrays.asList(askId.split(","));
        }
        StringBuilder askIdsBuilder = new StringBuilder(askId);
        for (String giftAskId : promotionAskIds) {
            askIdsBuilder.append(",").append(giftAskId);
        }

        for (String askid : latterAskIds) {
            askIdsBuilder.append(",").append(askid);
        }

        askId = askIdsBuilder.toString();

        return listLatterAskIds(askId, ownerCustId);
    }

    @Override
    public List<String> listLatterAskIds(String askId, String ownerCustId) {
        if (StringUtils.isBlank(askId)) {
            return new ArrayList<>();
        }
        List<String> askIds = Arrays.asList(askId.split(","));
        List<String> latterAskIds = this.orderItemMapper.listLatterAskIds(askIds, ownerCustId);

        if (KiteListUtils.isEmpty(latterAskIds)) {
            return askIds;
        }

        StringBuilder askIdsBuilder = new StringBuilder(askId);
        for (String askid : latterAskIds) {
            askIdsBuilder.append(",").append(askid);
        }
        askId = askIdsBuilder.toString();
        return listLatterAskIds(askId, ownerCustId);
    }

    @Override
    public List<String> listOrderItemIdsByAskIds(List<String> askIds, String ownerCustId) {
        return this.orderItemMapper.listOrderItemIdsByAskIds(askIds, ownerCustId);
    }

    @Override
    public List<String> listFacthAskIds(List<String> askIds, String ownerCustId) {
        return this.orderItemMapper.listFacthAskIds(askIds, ownerCustId);
    }

    @Override
    public List<String> listRelativeAskIds(List<String> askIds, String ownerCustId) {
        return this.orderItemMapper.listRelativeAskIds(askIds, ownerCustId);
    }

    @Override
    public List<String> listRelCompInstIdAskIds(List<String> askIds, String ownerCustId) {
        List<String> askid = this.orderItemMapper.listRelCompInstIdAskIds(askIds, ownerCustId);
        if (KiteListUtils.isEmpty(askid)) {
            return askIds;
        }
        askIds.addAll(askid);
        return listRelCompInstIdAskIds(askIds, ownerCustId);
    }

}
